package com.im.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 对象转换工具类
 * @author fortune
 */
public class ModelToVoUtil {

	private static final Logger LOG = LoggerFactory.getLogger(ModelToVoUtil.class);

	/**
	 * 两个已经实例化的对象，model转值到vo.
	 * 
	 * @param model
	 * @param vo
	 */
	public static void modelToVo(Object model,Object vo){
		Class<? extends Object> modelClss = model.getClass();
		Class<? extends Object> voClss = vo.getClass();
		try{

			Method[] modelMethods = modelClss.getMethods();
			for(Method modelMethod : modelMethods){
				String modelMethodName = modelMethod.getName();
				if(modelMethodName.startsWith("get")){
					Object val = modelMethod.invoke(model,new Object[]{});
					Method[] voMethods = voClss.getMethods();
					for(Method voMethod : voMethods){
						String voMethodName = voMethod.getName();
						if(voMethodName.startsWith("set") && modelMethodName.indexOf(voMethodName.substring(1)) != -1){
							if(voMethodName.length() == modelMethodName.length()){
								voMethod.invoke(vo,new Object[]{val});
							}

						}
					}
				}
			}

		}catch(InvocationTargetException e){
			LOG.debug(e.getMessage());
		}catch(IllegalArgumentException e){
			LOG.debug(e.getMessage());
		}catch(IllegalAccessException e){
			LOG.debug(e.getMessage());
		}
	}

	/**
	 * 根据model生成VO对象或根据List<model>生成List<vo>对象.
	 * 
	 * @param model
	 * @param voClss
	 * @return obj
	 */
	@SuppressWarnings("unchecked")
	public static Object createVoFromModel(Object model,Class<?> voClss){

		Class<?> modelClss = model.getClass();
		Object vo = null;
		List<Object> modelList = null;
		List<Object> voList = null;
		try{
			if(model instanceof List){
				modelList = (List<Object>)model;
				voList = new ArrayList<>();
				for(Object obj : modelList){
					modelClss = obj.getClass();
					vo = voClss.newInstance();
					Method[] modelMethods = modelClss.getMethods();
					for(Method modelMethod : modelMethods){
						String modelMethodName = modelMethod.getName();
						if(modelMethodName.startsWith("get")){
							Object val = modelMethod.invoke(obj,new Object[]{});
							Method[] voMethods = voClss.getMethods();

							for(Method voMethod : voMethods){
								String voMethodName = voMethod.getName();
								if(voMethodName.startsWith("set")
								        && modelMethodName.substring(1).equals(voMethodName.substring(1))){
									voMethod.invoke(vo,new Object[]{val});
								}
							}
						}
					}

					voList.add(vo);
				}
				vo = voList;
			}else{
				vo = voClss.newInstance();
				Method[] modelMethods = modelClss.getMethods();
				for(Method modelMethod : modelMethods){
					String modelMethodName = modelMethod.getName();
					if(modelMethodName.startsWith("get")){
						Object val = modelMethod.invoke(model,new Object[]{});
						Method[] voMethods = voClss.getMethods();

						for(Method voMethod : voMethods){
							String voMethodName = voMethod.getName();
							if(voMethodName.startsWith("set")
							        && modelMethodName.substring(1).equals(voMethodName.substring(1))){
								voMethod.invoke(vo,new Object[]{val});
							}
						}
					}
				}
			}

		}catch(InvocationTargetException e){
			LOG.debug(e.getMessage());
		}catch(IllegalArgumentException e){
			LOG.debug(e.getMessage());
		}catch(IllegalAccessException e){
			LOG.debug(e.getMessage());
		}catch(InstantiationException e){
			LOG.debug(e.getMessage());
		}

		return vo;
	}

	/*
	 * public static void main(String[] args){ User model = new User();
	 * List<User> users = new ArrayList<>(); users.add(model);
	 * model.setName("qlg"); List<User> list =
	 * (List<User>)createVoFromModel(users,User.class); }
	 */
}
